package org.ysh.core.attachment;

import org.ysh.annotation.AdditionInfoResolver;
import org.apache.poi.hslf.usermodel.HSLFSlideShow;

import java.io.File;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Date;

@AdditionInfoResolver(extensions = {"ppt"})
public class Powerpoint2003AdditionInfoResolver implements AttachmentAdditionInfoResolver {

    @Override
    public void addAdditionInfo(File tmpFile, AdditionInfo additionInfo) throws Exception {
        try(HSLFSlideShow hslfSlideShow = new HSLFSlideShow(Files.newInputStream(tmpFile.toPath()));
            OutputStream fos = Files.newOutputStream(tmpFile.toPath())){
            PoiHelper.writeCustomInfo(hslfSlideShow.getDocumentSummaryInformation(),additionInfo);
            hslfSlideShow.write(fos);
        }

    }

    @Override
    public AdditionInfo readAdditionInfo(InputStream in) throws Exception {
        try(HSLFSlideShow hslfSlideShow = new HSLFSlideShow(in)){
           return PoiHelper.readAdditionInfo(hslfSlideShow.getDocumentSummaryInformation());
        }
    }

    public static void main(String[] args) throws Exception {
        Powerpoint2003AdditionInfoResolver powerpoint2003AdditionInfo = new Powerpoint2003AdditionInfoResolver();
        AdditionInfo additionInfo = new AdditionInfo();
        additionInfo.setUserName("Lisssi");
        additionInfo.setTime(new Date());
        additionInfo.setHost("192.168.1.1");
        powerpoint2003AdditionInfo.addAdditionInfo(new File("D:\\1123.ppt"),additionInfo);

        additionInfo = powerpoint2003AdditionInfo.readAdditionInfo(Files.newInputStream(Paths.get("D:\\1123.ppt")));
        System.out.println(additionInfo);
    }
}
